package com.memcached.utils;

import java.util.Enumeration;
import java.util.Properties;
import java.util.PropertyResourceBundle;

/**
 * 
 * @description: 读取properties 文件功能类
 * @author: 
 * @date： 2011-4-26 下午04:06:48
 * 
 * 
 */
public class MemcConfigLoader {
	static private String path = "memc";
	private static Properties sysProperty = null;

	/**
	 * 获取配置信息
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {

		return getProperty(key, "");
	}

	public static String getProperty(String key, String defaultValue) {

		if (sysProperty == null) {
			sysProperty = getPropObjFromBundle(path);
		}

		return sysProperty.getProperty(key, defaultValue);
	}

	public static int getProperty(String key, int value) {
		String val = getProperty(key, value + "");
		return (val == null) ? value : Integer.parseInt(val);
	}

	public static Properties getPropObjFromBundle(String bundleName) {

		Properties objProp = new Properties();

		PropertyResourceBundle bundle = (PropertyResourceBundle) PropertyResourceBundle
				.getBundle(bundleName);
		Enumeration<?> enm = bundle.getKeys();
		while (enm.hasMoreElements()) {
			String key = (String) enm.nextElement();
			String value = bundle.getString(key);
			objProp.setProperty(key, value);

		}
		return objProp;
	}
}
